package com.wigem.fa.service;

import java.util.List;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wigem.fa.dto.ApplyAndEntInfoDto;
import com.wigem.framework.common.pojo.PageResult;
import com.wigem.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.wigem.fa.entity.FaSupervisorEntity;
import com.wigem.fa.mapper.FaSupervisorMapper;
import com.wigem.fa.vo.FaSupervisorVo;
import com.wigem.util.MappingResultUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * 项目督导Service业务层处理
 *
 * @author HH
 * @date 2025-02-21
 */
@Service
public class FaSupervisorServiceImpl implements FaSupervisorService {

    @Resource
    private FaSupervisorMapper faSupervisorMapper;


    @Override
    public List<FaSupervisorEntity> getList(FaSupervisorVo faSupervisorvo) {

        return faSupervisorMapper.selectList(query(faSupervisorvo));
    }

    @Override
    public PageResult<FaSupervisorEntity> getPageList(FaSupervisorVo faSupervisorVo) {
        faSupervisorVo.setOrderColumn(MappingResultUtils.property2Field(faSupervisorVo.getOrderColumn(), ApplyAndEntInfoDto.class));
        Page<FaSupervisorEntity> page = new Page<>(faSupervisorVo.getPageNo(), faSupervisorVo.getPageSize());
        IPage<FaSupervisorEntity> mappingList = faSupervisorMapper.getList(page, faSupervisorVo);
        return new PageResult<>(mappingList.getRecords(), mappingList.getTotal());
    }

    @Override
    public PageResult<FaSupervisorEntity> getSummaryPageList(FaSupervisorVo faSupervisorVo) {
        faSupervisorVo.setOrderColumn(MappingResultUtils.property2Field(faSupervisorVo.getOrderColumn(), ApplyAndEntInfoDto.class));
        Page<FaSupervisorEntity> page = new Page<>(faSupervisorVo.getPageNo(), faSupervisorVo.getPageSize());
        IPage<FaSupervisorEntity> mappingList = faSupervisorMapper.getSummaryPageList(page, faSupervisorVo);
        return new PageResult<>(mappingList.getRecords(), mappingList.getTotal());
    }


    @Override
    public FaSupervisorEntity getEntity(String id) {
        return faSupervisorMapper.selectById(id);
    }

    @Override
    public FaSupervisorEntity queryEntity(FaSupervisorVo faSupervisorVo) {
        return faSupervisorMapper.queryEntity(faSupervisorVo);
    }

    @Override
    public void add(FaSupervisorVo faSupervisorVo) {
        FaSupervisorEntity faSupervisorEntity = new FaSupervisorEntity();
        BeanUtils.copyProperties(faSupervisorVo, faSupervisorEntity);
        faSupervisorEntity.create();
        faSupervisorMapper.insert(faSupervisorEntity);
    }


    @Override
    public void update(FaSupervisorVo faSupervisorVo) {
        FaSupervisorEntity faSupervisorEntity = new FaSupervisorEntity();
        BeanUtils.copyProperties(faSupervisorVo, faSupervisorEntity);
        faSupervisorMapper.updateById(faSupervisorEntity);
    }

    @Override
    public void save(FaSupervisorVo faSupervisorVo) {

        FaSupervisorEntity faSupervisorEntity = new FaSupervisorEntity();
        BeanUtils.copyProperties(faSupervisorVo, faSupervisorEntity);
        if (StringUtils.isEmpty(faSupervisorVo.getId())) {
            faSupervisorEntity.create();
            faSupervisorEntity.setRemoveMark("0");
            faSupervisorMapper.insert(faSupervisorEntity);
        } else {
            faSupervisorMapper.updateById(faSupervisorEntity);
        }

    }


    @Override
    public int delete(String id) {
        return faSupervisorMapper.deleteById(id);
    }


    @Override
    public int remove(String id) {
        return faSupervisorMapper.remove(id);
    }


    public LambdaQueryWrapperX<FaSupervisorEntity> query(FaSupervisorVo faSupervisorVo) {

        LambdaQueryWrapperX<FaSupervisorEntity> faSupervisorEntityLambdaQueryWrapperX = new LambdaQueryWrapperX<>();
        faSupervisorEntityLambdaQueryWrapperX = faSupervisorEntityLambdaQueryWrapperX
                .eqIfPresent(FaSupervisorEntity::getId, faSupervisorVo.getId())
                .eqIfPresent(FaSupervisorEntity::getEntId, faSupervisorVo.getEntId())
                .eqIfPresent(FaSupervisorEntity::getReportId, faSupervisorVo.getReportId())
                .eqIfPresent(FaSupervisorEntity::getSupervisorTime, faSupervisorVo.getSupervisorTime())
                .eqIfPresent(FaSupervisorEntity::getSupervisorItem, faSupervisorVo.getSupervisorItem())
                .eqIfPresent(FaSupervisorEntity::getEntFeedback, faSupervisorVo.getEntFeedback())
                .eqIfPresent(FaSupervisorEntity::getRemoveMark, faSupervisorVo.getRemoveMark())

        ;
        return faSupervisorEntityLambdaQueryWrapperX;
    }


}
